package com.wu.controller;

import com.wu.error.BusinessException;
import com.wu.error.EnumBusinessError;
import com.wu.response.CommonReturnType;
import com.wu.util.FTPUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-04-28 15:44
 */
@RestController
@RequestMapping(value = "/file", method = RequestMethod.POST)
@Api(tags = "写文章时候的图片上传")
public class FileController {

    @Autowired
    private FTPUtil ftpUtil;

    /**
     * @param file
     * @return
     */
    @ApiOperation(value = "上传（与用户头像分开）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataTypeClass = MultipartFile.class)
    })
    @RequestMapping("/upload")
    public CommonReturnType tokenUploadArticleFile(@RequestParam(name = "file") MultipartFile file) {
        String path;
        if (file != null && !file.isEmpty()) {
            path = ftpUtil.uploadFolderWithArticle(file);
        } else {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "文件上传失败");
        }
        return CommonReturnType.create(path);
    }

}
